import { data } from './../views/demo/excel/data';
import { columns } from './../views/demo/system/vxe-account/vxeAccount.data';
import { defHttp } from '@/utils/http/axios';

import { ErrorMessageMode } from '#/axios';

enum Api {
  getDatasourcesRecordList = '/getDatasourcesRecordList',
  saveDatasourcesRecord = '/saveDatasourcesRecord',
  deleteOneRecord = '/datasources/deleteDatasourcesRecord',
  getDatabasesList = '/databases/getDatabases',
  getTableList = '/tables/getTableList',
  getTableInfo = '/tables/getTableInfo',
  createTable = '/tables/createTable',
  updateTable = '/tables/updateTable',
  dropTable = '/tables/dropTable',
  renameTable = '/tables/renameTable',

  getFieldsList = '/fields/getFields',
  updateOneField = '/fields/updateOneField',
  addOneField = '/fields/addOneField',
  deleteOneField = '/fields/deleteOneField',
  execRawSql = '/execRawSql',

  // lsp
  lspComplete = '/lsp/complete',

  // table relationship
  getTableRelList = '/tables_rel/getTableRelList',
  addTableRel = '/tables_rel/addTableRel',
  updateTableRel = '/tables_rel/updateTableRel',
  deleteTableRel = '/tables_rel/deleteTableRel',
}

export interface DatasourcesObject {
  ID: number;
  userId: number;
  name: string;
  ip: string;
  port: number;
  user: string;
  password: string;
  database: string;
}

/**
 * @description: 获取数据源记录列表
 */
export async function getDatasourcesList(
  params: { pageNo: number; pageSize: number },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.get<DatasourcesObject[]>(
    {
      url: Api.getDatasourcesRecordList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 增改，保存一条记录 */
export async function saveOneDatasourcesRecord(
  params: DatasourcesObject,
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<DatasourcesObject>(
    {
      url: Api.saveDatasourcesRecord,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 删除一条记录 */
export async function deleteOneDatasourcesRecord(
  params: { id: number },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<DatasourcesObject>(
    {
      url: Api.deleteOneRecord,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 获取一个数据源下的数据库列表 */
export async function getDatabasesList(
  params: { dsId: number },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.get<{ name: string }[]>(
    {
      url: Api.getDatabasesList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 获取一个数据库的表列表 */
export async function getTableList(
  params: { dsId: number; databaseName: string },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ tables: { name: string; comment?: string }[] }>(
    {
      url: Api.getTableList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}


export async function getTableInfo(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ table: { name: string; comment?: string, engine?: string, charset?: string, collation?: string }, fields: IFieldObject[]}>(
    {
      url: Api.getTableInfo,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 创建一个表 */
export async function createTable(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
    fields: IFieldObject[];
    tableArgs: Record<string, any>;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ tables: { name: string; comment?: string }[] }>(
    {
      url: Api.createTable,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function updateTable(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
    newTableName: string;
    fields?: IFieldObject[];
    tableArgs?: Record<string, any>;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{
    tableName: string;
    alterSQL: string;
  }>(
    {
      url: Api.updateTable,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 删除一个表 */
export async function dropTable(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ tables: { name: string; comment?: string }[] }>(
    {
      url: Api.dropTable,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 重命名一个表 */
export async function renameTable(
  params: {
    dsId: number;
    databaseName: string;
    oldTableName: string;
    newTableName: string;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ tables: { name: string; comment?: string }[] }>(
    {
      url: Api.renameTable,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 执行原始sql */
export async function execRawSql(
  params: { dsId: number; databaseName: string; sql: string },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{ affectedRows: number; result: any[]; headers: string[] }>(
    {
      url: Api.execRawSql,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** lsp complete */
export async function lspComplete(
  params: { dsId: number; databaseName: string; input: string; line: number; col: number },
  mode: ErrorMessageMode = 'message',
) {
  /** return struct: {
        "label": "createtime",
        "kind": 5,
        "detail": "column from \"admin_auth_dept\"",
        "documentation": {
            "kind": "markdown",
            "value": "`admin_auth_dept`.`createtime` column\n\n`int`\n"
        },
        "sortText": "0createtime"
    }, */
  return defHttp.post<{ label: string; kind: number; detail: string; documentation: { kind: string; value: string }; sortText: string }[]>(
    {
      url: Api.lspComplete,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/** 字段信息对象 */
export interface IFieldObject {
  name: string;
  type: string;
  dataType: string;
  length: number;
  isUnsigned: boolean;
  isAutoIncrement: boolean;
  isNull: boolean;
  isNotNull: boolean;
  comment: string;
  default: string;
  isPrimary: boolean;
}

export async function getFieldsList(
  params: { dsId: number; databaseName: string; tableName: string },
  mode: ErrorMessageMode = 'message',
) {
  const fields = await defHttp.post<IFieldObject[]>(
    {
      url: Api.getFieldsList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
  fields.forEach((i) => {
    i.isNotNull = !i.isNull;
  });
  return fields;
}

export async function addOneField(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
    field: IFieldObject;
    afterField?: string;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<IFieldObject[]>(
    {
      url: Api.addOneField,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function updateOneField(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
    oldFieldName: string;
    field: IFieldObject;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<IFieldObject[]>(
    {
      url: Api.updateOneField,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function deleteOneField(
  params: {
    dsId: number;
    databaseName: string;
    tableName: string;
    fieldName: string;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<IFieldObject[]>(
    {
      url: Api.deleteOneField,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

// table relationship
export async function getTableRelList(
  params: { dsId: number; database: string; table: string },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<{
    id: number;
    name: string;
    database: string;
    sourceTable: string;
    targetTable: string;
    type: string;
    relationships: {
      id: number;
      sourceTableField: string;
      targetTableField: string;
    }[];
  }[]>(
    {
      url: Api.getTableRelList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function addTableRel(
  params: {
    name: string;
    datasourceId: number;
    database: string;
    sourceTable: string;
    targetTable: string;
    type: string;
    relationships: {
      sourceTableField: string;
      targetTableField: string;
    }[];
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<boolean>(
    {
      url: Api.addTableRel,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function updateTableRel(
  params: {
    id: number;
    relationships: {
      id?: number;
      sourceTableField: string;
      targetTableField: string;
    }[];
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<boolean>(
    {
      url: Api.updateTableRel,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function deleteTableRel(
  params: {
    dsId: number;
    id: number;
  },
  mode: ErrorMessageMode = 'message',
) {
  return defHttp.post<boolean>(
    {
      url: Api.deleteTableRel,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}